package com.yonyou.pmclouds.problemlog.process;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.problemlog.entity.ProblemLogVO;
import com.yonyou.pmclouds.projectlog.entity.ProblemStatisticVo;
import com.yonyou.pmclouds.projectlog.entity.ProjectLogDetailVo;
import com.yonyou.pmclouds.projectlog.entity.ProjectLogJobVo;
import com.yonyou.pmclouds.projectlog.rmiitf.ProjectLogMaintain;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;

/**
 * 智能日志记录维护后置处理器，新建或更新都使用
 * 出错不要影响其他本来的流程
 * @author xiejianyu
 * 2019-09-23
 */
@Slf4j
@Component
public class ProblemLogProjectLogPostProcess implements SingleProcessor<ProblemLogVO> {

    private static ObjectMapper objectMapper = new ObjectMapper();
    @Autowired
    private ProjectLogMaintain projectLogMaintain;

    @Override
    public void process(ProblemLogVO vo, ProblemLogVO orig) throws BusinessException {
        //如果使用标准的 SingleInsert SingleUpdate SingleDelete 模板进行操作的话，新建时orig 为null，更新或删除时orig不为null
        try {
            if (orig==null) {//目前只有新增
                ProjectLogJobVo jobVo = new ProjectLogJobVo();//最终放入队列的任务对象

                jobVo.setPkTenant(vo.getPk_tenant());
                jobVo.setPkProject(vo.getPk_project());

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(vo.getCreationtime());
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                jobVo.setLogDate(calendar.getTime());

                jobVo.setEventType(ProjectLogJobVo.EVENT_TYPE_DETAIL);
                jobVo.setEventTime(vo.getCreationtime());
                jobVo.setUserId(vo.getCreator());

                ProblemStatisticVo problemStatisticVo = new ProblemStatisticVo();
                problemStatisticVo.setPkBill(vo.getPk_problem());
                problemStatisticVo.setBillType(BillTypeConst.PROBLEMLOG_BILLTYPE);
                problemStatisticVo.setCreatorName(vo.getCreator_name());
                problemStatisticVo.setProblem(vo.getContent());
                problemStatisticVo.setResolverName(vo.getRectificationer_name());

                ProjectLogDetailVo logDetail = new ProjectLogDetailVo();
                logDetail.setDetailType(ProjectLogDetailVo.PROBLEM_STATISTIC);
                logDetail.setPkContentSource(vo.getPk_problem());
                logDetail.setContentJson(objectMapper.writeValueAsString(problemStatisticVo));
                logDetail.setCreator(vo.getCreator());
                logDetail.setCreationtime(vo.getCreationtime());
                logDetail.setModifier(vo.getCreator());
                logDetail.setModifytime(vo.getCreationtime());

                jobVo.setAttachment(logDetail);
                //pmCloudCacheManager.addObjToBlockingQueue(ProjectLogJobVo.JOB_QUEUE_NAME, jobVo);
                projectLogMaintain.upsert(jobVo);
            }
        } catch (Exception e) {
            log.error("问题记录单--智能日志处理出错，记录pk：{}", vo.getPk_problem());
            log.error("异常信息", e);
        }
    }
}
